var rootPath = getRootPath();
var pageNo = 1 ;
var pageSize = 10;
$(function() {
	pageNo = 1 ;
	var val = decodeURI(window.location.search).substring(
			decodeURI(window.location.search).indexOf("=") + 1);
	if (val && $.trim(val) != "") {
		$("#focusSearch").val(val);
		$(".removeIcon").show();
		solrSearch(val);
	} else {
		$("#focusSearch").val("");
		$(".removeIcon").hide();
	}
	// 搜索聚焦
	$(".removeIcon").hide();

	//键盘监控
	$("#focusSearch").keypress(function(e) {
		 if (e.which == 13) {
			 $(location).prop('href', encodeURI('/account/normal/pages/search/search.html?content='+$("#focusSearch").val()));  
         }
	});
	// 删除搜索数据
	$(".search-warpper .removeIcon").click(function() {
		$("#focusSearch").prop("value", "");
		$(".serachIcon").show();
		$(".removeIcon").hide();
	});
	
	$("#button").click(function(){
		$(location).prop('href', encodeURI('/account/normal/pages/search/search.html?content='+$("#focusSearch").val()));
	});
	$("#source").change(function(){
		pageNo = 1;
		solrSearch($("#focusSearch").val());
	});
	$("#time").change(function(){
		pageNo = 1;
		solrSearch($("#focusSearch").val());
	});
	$("#sort").change(function(){
		pageNo = 1;
		solrSearch($("#focusSearch").val());
	});
	
});
function focusSearch(e){
	if(e !== ''){
		$(".removeIcon").show();
	}else{
		$(".removeIcon").hide();
	}
	
}
function solrSearch(searchValue) {
		$("#result").children().remove();
		// var where = $("#focusSearch").val();
		if (searchValue == null || searchValue == "") {
			return;
		}
		var source = $("#source").val();
		var time = $("#time").val();
		var sort = $("#sort").val();
		$.ajax({
			type : 'POST',
			
			url : rootPath + '/solr/highlightSearch',
			dataType : 'json',
			contentType : 'application/json;charset=UTF-8',
			data : JSON.stringify({
				"pageNo" : pageNo,
				"pageSize" : pageSize,
				"searchValue" : searchValue,
				"source":source,
				"time":time,
				"sort":sort
			}),
			success : function(res) {
				 if (res.returnCode == "0000") {
					var resultNumFound = res.returnObject.resultNumFound;
					$("#resultNumFound").html(resultNumFound);
					var resultDiv = $("#result");
					$.each(res.returnObject.resultList, function(index, value) {
						var courseDiv = $("<div class='resultItem'></div>");
						var titleSuffix = "-";
						if(value.source=="1"){
							titleSuffix +="课程卡片";
						}else if(value.source=="2"){
							titleSuffix +="梦想盒子"; 
						}else{
						
						}
						var titleDiv = $("<span><a class='resultTitle' href='"+value.url+"'>"+$.trim(value.title)+"</a></span><span class='titleSuffix'>"+titleSuffix+"</span</div>");
	 
						titleDiv.appendTo(courseDiv);

						var authorDiv = $("<div><p class='marginTop'>作者："
								+ value.author + "</p></div>");
						authorDiv.appendTo(courseDiv);

						if (value.tags) {
							var tagsDiv = $("<div><p class='marginTop'>标签："
									+ value.tags + "</p></div>");
							tagsDiv.appendTo(courseDiv);
						}
						var timeDiv = $("<div><p class='marginTop'>创建时间："
								+ new Date(parseInt(value.time)).Format("yyyy-MM-dd hh:mm:ss") + "</p></div>");
						timeDiv.appendTo(courseDiv);
						var contentDiv = $("<div><p class='marginTop resultContent'>"
								+ $.trim(value.content) + "</p></div>");
						contentDiv.appendTo(courseDiv);

						courseDiv.appendTo(resultDiv);
						/*resultDiv.append("<hr></hr>");*/						
					});
					//页脚
					$("#page").empty();
					if(resultNumFound>0){
						var page = ""; 
						/*page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
							+ 1 + ")'>首页</span>";*/
						if (pageNo>1) {
							page += "<span class='pageNavigate pageNo' onclick='switchPage("
									+ (pageNo-1) + ")'>上一页</span>";
						}
						var num = parseInt(resultNumFound / pageSize) ;
						if(resultNumFound%pageSize != 0){
							num += 1;
						}
						var a=1,b=num+1;
						if(num>10){
							if(pageNo>5){
								if(pageNo+5<num){
									a=pageNo-5;
									b=pageNo+5;
								}else{
									a=num-9;
								}
							}else{
								b=11;
							}	
						} 
		
					 
						for (var i = a; i < b; i++) {
							if( pageNo == i){
								page += "<span class='pageNo border-none' style='font-weight:900'>"
									+ i + "</span>";
							}else{
								page += "<span  class='pageNavigate pageNo' onclick='switchPage("
									+ i
									+ ")'>"
									+ i + "</span>";
							}
						}
						if (pageNo< num) {
							page += "<span  class='pageNavigate pageNo' onclick='switchPage("
									+ (pageNo+1) + ")'>下一页</span>";
						}
						
						$("#page").append(page);
						$(".pageNavigate").mouseover(function() {
							$(this).css("cursor", "pointer");
						});
					} 
				 
				} else {
					console.error("错误提示")
				}

			},
			fail : function() {
				console.error("error");
			}

		});
	};

 Date.prototype.Format = function(fmt) {  
  var o = {   
    "M+" : this.getMonth()+1,                 // 月份
    "d+" : this.getDate(),                    // 日
    "h+" : this.getHours(),                   // 小时
    "m+" : this.getMinutes(),                 // 分
    "s+" : this.getSeconds(),                 // 秒
    "q+" : Math.floor((this.getMonth()+3)/3), // 季度
    "S"  : this.getMilliseconds()             // 毫秒
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
  fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
 } ;
function switchPage(num){
	$(this).css("cursor", "pointer");
	pageNo = num ;
	solrSearch($("#focusSearch").val());
	$(".pageNavigate").hover(function() {
		$(this).css("cursor", "pointer");
	});
}

